﻿@using CarparkBusiness
@model IEnumerable<CarparkIMS_CMS.Models.CameraConfigModel>

@{
    ViewData["Title"] = "Index";
    BackgroundServiceStatus bgService = (BackgroundServiceStatus)ViewBag.BackgroundServiceStatus;
    string bgServiceStatusLbl = bgService.IsStopped ? "SERVICE STOPPED" : "IN SERVICE";
}
<h1>Index</h1>
<p>
    <a asp-action="Create" class="btn btn-sm text-danger" style="font-weight:600;"> <i class="far fa-plus-square mr-1"></i>CREATE NEW</a>&nbsp;&nbsp;

    <a asp-action="StopScopedService" asp-controller="Home" class="btn btn-sm text-black-50" target="_blank">&nbsp;.&nbsp;</a>
    <a href="GetParkingSetting" style="font-weight:600;"> <i class="fas fa-cog mr-1"></i>Setting</a>
    <button id="StopBtn" class="btn btn-sm text-danger p-1" style="font-weight:600;" data-isstopped="@bgService.IsStopped" data-stoppedon="@string.Format("{0:yyyy-MM-ddTHH:mm:ss}",bgService.StoppedOn)")">
        <i class="fas fa-stop mr-1"></i>@bgServiceStatusLbl
    </button>
   
</p>
<table class="table table-responsive">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CameraIP)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DoorName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TransactionType)
            </th>
            <th>
                Parking Place Id
            </th>
            <th>
                Parking Place Number
            </th>
            <th>To Do</th>
        </tr>
    </thead>
    <tbody>
            @foreach (var item in Model) {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.CameraIP)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.DoorName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.TransactionType)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.ParkingPlaceId)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.ParkingPlaceSharpNumber)
                        </td>
                        <td>
                            <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
                            <a asp-action="Details" asp-route-id="@item.ID">Details</a> |
                            <a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
                        </td>
                    </tr>
            }
    </tbody>
</table>
<script>
   
    $("#StopBtn").click(function () {

        var isStop = $("#StopBtn").data("isstopped");
        console.log("IsStop = " + isStop);

        $.get("/Home/StopBackGround", function (ret, status) { 
            console.log(ret); 
            if (status != undefined) {
                if (status == "success") { 
                    console.log({ "status": status }); 
                    $("#StopBtn").data("isstopped", ret.data.isStopped);
                    $("#StopBtn").data("stoppedon", ret.data.stoppedOn);

                    layer.open({
                        title: 'Return'
                        , content: ret.meta.message + "<br>STOP=" + ret.data.isStopped
                        , btn: 'OK'
                    });

                    if (ret.data.isStopped )
                    {
                        $("#StopBtn").html("<i class='fas fa-stop mr-1'></i> SERVICE STOPPED");
                    }else{
                        $("#StopBtn").html("<i class='fas fa-play mr-1'></i> IN SERVICE");
                    }
                }else{ 
                    layer.msg("NETWORK ERROR!");
                }
            } else {
                layer.msg("NETWORK ERROR!");
            }
        });
    });
</script>